package cn.lf.android.lawfirm.http.manager;

import android.util.Pair;

import java.io.File;
import java.util.List;
import java.util.Map;

import cn.yzl.okhttputils.okhttp.OkHttpUtils;
import cn.yzl.okhttputils.okhttp.builder.GetBuilder;
import cn.yzl.okhttputils.okhttp.builder.PostFormBuilder;
import cn.yzl.okhttputils.okhttp.callback.Callback;
import cn.yzl.okhttputils.okhttp.request.RequestCall;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author zby
 * @version create time：2016-8-11 下午7:03:36
 */
public class MyOkHttpClient {

    private RequestCall build;

    public void doGet(String url, Callback<String> callback) {
        GetBuilder builder = OkHttpUtils.get().url(url);
        builder.build().execute(callback);
    }

    public void doGet(String url, Map<String, String> params, Callback<String> callback) {
        GetBuilder builder = OkHttpUtils.get().url(url);
        if (params != null) {
            for (String key : params.keySet()) {
                builder.addParams(key, params.get(key));
            }
        }
        builder.build().execute(callback);
    }

    /**
     * 带头请求
     *
     * @param url
     * @param params
     * @param callback
     * @param headerKey
     * @param header
     */
    public void doGet(String url, Map<String, String> params, Callback<String> callback, String headerKey, String header) {
        GetBuilder builder = OkHttpUtils.get().url(url);
        if (params != null) {
            for (String key : params.keySet()) {
                builder.addParams(key, params.get(key));
                builder.addHeader(headerKey, header);
            }
        }
        builder.build().execute(callback);
    }

    /**
     * 带头请求
     *
     * @param url
     * @param params
     * @param callback
     * @param headerKey
     * @param header
     */
    public void doPost(String url, Map<String, String> params, Callback<String> callback, String headerKey, String header) {
        PostFormBuilder builder = OkHttpUtils.post().url(url);
        if (params != null) {
            for (String key : params.keySet()) {
                builder.addParams(key, params.get(key));
                builder.addHeader(headerKey, header);
            }
        }
        builder.build().execute(callback);
    }

    public void doPost(String url, Map<String, String> params, Callback<String> callback) {
        PostFormBuilder builder = OkHttpUtils.post().url(url);
        if (params != null) {
            for (String key : params.keySet()) {
                builder.addParams(key, params.get(key));
            }
        }
        builder.build().execute(callback);
    }

    public void uploadFiles(String url, Map<String, String> params
            , List<Pair<String, File>> files, Callback<String> callback, String headerKey, String header) {

        PostFormBuilder builder = OkHttpUtils.post().url(url);

        for (String key : params.keySet()) {
            builder.addHeader(headerKey, header);
            builder.addParams(key, params.get(key));
        }
        for (Pair<String, File> pair : files) {
            builder.addFile(pair.first, pair.second.getName(), pair.second);
        }
        build = builder.build();
        build.execute(callback);
    }

    public void clearBuild() {
        if (null != build) {
            build.cancel();
        }
    }

    public void uploadFiles(String url, Map<String, String> params
            , List<Pair<String, File>> files, Callback<String> callback) {

        PostFormBuilder builder = OkHttpUtils.post().url(url);

        for (String key : params.keySet()) {
            builder.addParams(key, params.get(key));
        }
        for (Pair<String, File> pair : files) {
            builder.addFile(pair.first, pair.second.getName(), pair.second);
        }
        build = builder.build();
        build.execute(callback);
    }
}
